package P3排序算法;

import java.util.Arrays;
import java.util.Random;

public class MonkeySort {
    public static void main(String[] args) {
        int[] arr = {1, 5, 2, 3, 4, 9, 8, 6, 7};
        monkeySort(arr);
        System.out.println(Arrays.toString(arr));
    }

    public static void monkeySort(int[] arr) {
        Random random = new Random();
        while (!check(arr)) {
            int i = random.nextInt(arr.length);
            int j = random.nextInt(arr.length);
            swap(arr,i,j);
        }

    }

    private static boolean check(int[] arr) {
        for (int i = 1; i < arr.length; i++) {
            if (arr[i - 1] > arr[i]) {
                return false;
            }
        }
        return true;
    }

    private static void swap(int[] arr, int k, int j) {
        int temp = arr[k];
        arr[k] = arr[j];
        arr[j] = temp;
    }
}